import { login } from '@/api/user'
const state = {
  user: localStorage.getItem('user') || {},
  token: localStorage.getItem('token-hnfb-69') || ''
}
const mutations = {
  upDateuser(state, user) {
    state.user = user
    state.token = localStorage.getItem('token-hnfb-69')
  },
  clearToken(state) {
    state.token = ''
    state.user = {}
  }
}
const actions = {
  async login(store, user) {
    const res = await login(user)
    console.log(res, 19)
    localStorage.setItem('loginTime', Date.now())
    if (res) {
      localStorage.setItem('token-hnfb-69', res.data.jwt)
      localStorage.setItem('user', JSON.stringify(res.data.user))
      await store.commit('upDateuser', res.data.user)
    }
  },
  async logout(store) {
    localStorage.setItem('token-hnfb-69', '')
    localStorage.setItem('user', '')
    await store.commit('clearToken')
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
